<?php
namespace Home\Controller;
use Think\Controller;


class UserController extends Controller{
        public function register(){
               if(!IS_POST){
                $this->display();
              }else{
                $user = D('user');
                if(!$user->create()){

                  echo $user->getError();
                }else{
                //print_r($user->create());exit;
                echo $user->reg() ? 'ok':'fail';
              }
           }     
               
        }

        public function login(){
             if(!IS_POST){
              $this->display('register');
             }else{
              //验证用户名和密码是否正确
              $user =D('user');
              $u = $user->where(array('username'=>I('post.username')))->find();
              if(empty($u)){
                exit('用户名不存在');
              }
              //print_r($u);exit;
              if(!$user->checkPass(I('post.password'))){
              echo '密码错误';
             }else{
              //print_r($user->checkPass());exit;
              //echo '登陆成功，设置cookie';
              $user->auth();
              $this->redirect('/');
             }
           }
        }


        public function logout(){
            $user  = D('user');
            $user ->qx();
            $this->redirect('Home/User/login');
        }

                
	   public function vcode(){
                  $Verify = new \Think\Verify();
                  $Verify -> imageW = 120; //验证码宽度
                  $Verify -> imageH = 40;  //验证码高度度
                  $Verify -> length  = 4;    // 验证码位数
                  $Verify -> fontSize = 16;    // 验证码字体大小
                  $Verify -> useNoise = false; // 关闭验证码杂点
                  $Verify->entry();

                 $this->display();
       }


        public function test(){
                    $this->display();
               }

}